<?php

class MediaFinder{
	
	// php doesn't allow constants that are arrays
	private static $VALID_TYPES = array( "image" );
	
	const MEDIA_SERVER_URL = "http://media.wii-social.com";
	
	public function MediaFinder($type, $name, $id){

		if ($type == null){
			$this->error("Media type not defined");
			return;
		}
		
		if ($name == null){
			$this->error("Media name not defined");
			return;
		}
		
		if ($id == null){
			$this->error("Media id not defined");
			return;
		}
		
		if (! in_array($type, self::$VALID_TYPES)){
			$this->error("Media type: " . $type . ", is not valid");
			return;
		}
		
		if($type == "image"){
			$this->fetchAndRedirectToImage($type, $name, $id);
		}
	}
	
	private function error($message){
		echo $message;
		return;
	}
	
	private static $IMAGE_TYPES = array( ".png" );
	
	private function fetchAndRedirectToImage($type, $name, $id){
		
		foreach(self::$IMAGE_TYPES as $t){
			
			$image = self::MEDIA_SERVER_URL . "/" . $name . "/" . strtolower($id) . $t;
			
			if (fopen($image, "r")){
				header("Location: " . $image);
				return;
			}
		}
		echo $image;
		$this->error("Image " . $id . $t . ", in the directory " . $name . " was not 
			found");
	}
}
$mf = new MediaFinder($_REQUEST['type'], $_REQUEST['name'], $_REQUEST['id']);
?>